package com.horizon.pojo;

import com.horizon.util.IdWorker;

/**
 * 创建IdWorker实例
 *
 * @author horizon
 */
public enum IdWorkerBuilder {

    ID_WORKER;

    private volatile IdWorker idWorker;

    public void build(long twepoch, long workerId, long datacenterId) {
        try {
            this.idWorker = new IdWorker(twepoch, workerId, datacenterId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public IdWorker getIdWorker() {
        if (this.idWorker == null) {
            throw new RuntimeException("method:getIdWorker exception===idWorker is null");
        }
        return this.idWorker;
    }

    public long nextId() {
        if (this.idWorker == null) {
            throw new RuntimeException("method:nextId exception===idWorker is null");
        }
        return this.idWorker.nextId();
    }

}
